const path = require('path');

// 生成index.html文件
const HtmlWebpackPlugin = require('html-webpack-plugin')

// 清理/dist文件
const CleanWebpackPlugin = require('clean-webpack-plugin')

// 删除未引用代码的插件
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  entry: './src/index.js',
  // entry: {
  //   app: './src/index.js',
  //   print: './src/print.js'
  // },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      // html中的title标签的内容
      title: 'production'
    }),
    new UglifyJSPlugin()
  ],
  output: {
    // filename: 'bundle.js',
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    // 也会在服务器脚本用到，以确保文件资源能够在 http://localhost:3000 下正确访问
    publicPath: '/'
  },
  module: {
    rules: [
      // 处理css文件
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      // 加载图片
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader'
        ]
      },
      // 加载字体
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          'file-loader'
        ]
      },
      // 加载数据
      {
        test: /\.(csv|tsv)$/,
        use: [
          'csv-loader'
        ]
      },
      {
        test: /\.xml$/,
        use: [
          'xml-loader'
        ]
      }

    ]
  }
}
